package com.lxc.trigger.http;

import com.lxc.domain.manage.service.IConfigManageService;
import com.lxc.domain.register.model.entity.ApplicationInterfaceEntity;
import com.lxc.domain.register.model.entity.ApplicationInterfaceMethodEntity;
import com.lxc.domain.register.model.entity.ApplicationSystemEntity;
import com.lxc.domain.register.service.IMessageService;
import com.lxc.domain.register.service.IRegisterManageService;
import com.lxc.types.common.Result;
import com.lxc.types.enums.ResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author lxc18
 * @date 2024/12/19 16:29
 * @description RegisterManageController
 */
@RestController
@RequestMapping("/wg/admin/register")

public class RpcRegisterManageController {
    private Logger logger = LoggerFactory.getLogger(RpcRegisterManageController.class);

    @Resource
    private IRegisterManageService registerManageService;
    @Resource
    private IMessageService messageService;
    @PostMapping(value = "registerApplication", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplication(@RequestParam String systemId,
                                               @RequestParam String systemName,
                                               @RequestParam String systemType,
                                               @RequestParam String systemRegistry) {
        try {
            logger.info("注册应用服务 systemId：{}", systemId);
            ApplicationSystemEntity applicationSystemEntity = new ApplicationSystemEntity();
            applicationSystemEntity.setSystemId(systemId);
            applicationSystemEntity.setSystemName(systemName);
            applicationSystemEntity.setSystemType(systemType);
            applicationSystemEntity.setSystemRegistry(systemRegistry);
            registerManageService.registerApplicationSystem(applicationSystemEntity);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (DuplicateKeyException e) {
            logger.warn("注册应用服务重复 systemId：{}", systemId, e);
            return new Result<>(ResponseCode.INDEX_DUP.getCode(), e.getMessage(), true);
        } catch (Exception e) {
            logger.error("注册应用服务失败 systemId：{}", systemId, e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }

    @PostMapping(value = "registerApplicationInterface", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplicationInterface(@RequestParam String systemId,
                                                        @RequestParam String interfaceId,
                                                        @RequestParam String interfaceName,
                                                        @RequestParam String interfaceVersion) {
        try {
            logger.info("注册应用接口 systemId：{} interfaceId：{}", systemId, interfaceId);
            ApplicationInterfaceEntity applicationInterfaceEntity = new ApplicationInterfaceEntity();
            applicationInterfaceEntity.setSystemId(systemId);
            applicationInterfaceEntity.setInterfaceId(interfaceId);
            applicationInterfaceEntity.setInterfaceName(interfaceName);
            applicationInterfaceEntity.setInterfaceVersion(interfaceVersion);
            registerManageService.registerApplicationInterface(applicationInterfaceEntity);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (DuplicateKeyException e) {
            logger.warn("注册应用接口重复 systemId：{} interfaceId：{}", systemId, interfaceId);
            return new Result<>(ResponseCode.INDEX_DUP.getCode(), e.getMessage(), true);
        } catch (Exception e) {
            logger.error("注册应用接口失败 systemId：{}", systemId, e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }
    @PostMapping(value = "registerApplicationInterfaceMethod", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplicationInterfaceMethod(@RequestParam String systemId,
                                                              @RequestParam String interfaceId,
                                                              @RequestParam String methodId,
                                                              @RequestParam String methodName,
                                                              @RequestParam String parameterType,
                                                              @RequestParam String uri,
                                                              @RequestParam String httpCommandType,
                                                              @RequestParam Integer auth) {
        try {
            logger.info("注册应用接口方法 systemId：{} interfaceId：{} methodId：{}", systemId, interfaceId, methodId);
            ApplicationInterfaceMethodEntity applicationInterfaceMethodEntity = new ApplicationInterfaceMethodEntity();
            applicationInterfaceMethodEntity.setSystemId(systemId);
            applicationInterfaceMethodEntity.setInterfaceId(interfaceId);
            applicationInterfaceMethodEntity.setMethodId(methodId);
            applicationInterfaceMethodEntity.setMethodName(methodName);
            applicationInterfaceMethodEntity.setParameterType(parameterType);
            applicationInterfaceMethodEntity.setUri(uri);
            applicationInterfaceMethodEntity.setHttpCommandType(httpCommandType);
            applicationInterfaceMethodEntity.setAuth(auth);
            registerManageService.registerApplicationInterfaceMethod(applicationInterfaceMethodEntity);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (DuplicateKeyException e) {
            logger.warn("注册应用接口重复 systemId：{} interfaceId：{}", systemId, interfaceId);
            return new Result<>(ResponseCode.INDEX_DUP.getCode(), e.getMessage(), true);
        } catch (Exception e) {
            logger.error("注册应用接口失败 systemId：{}", systemId, e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }
    @PostMapping(value = "registerEvent", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerEvent(@RequestParam String systemId) {
        try {
            logger.info("应用信息注册完成通知 systemId：{}", systemId);
            // 推送注册消息
            String gatewayId = registerManageService.getGatewayIdBySystemId(systemId);
            messageService.pushMessage(gatewayId, systemId);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (Exception e) {
            logger.error("应用信息注册完成通知失败 systemId：{}", systemId, e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }


}
